package com.ddstation.app.sewage.controller;

import com.ddstation.common.entity.DdPictureEntity;
import com.ddstation.common.mapper.DdPictureMapper;
import com.ddstation.common.service.InvsItemInstitutionService;
import com.ddstation.common.util.AliCloudOssUtil;
import com.ddstation.common.util.CheckUtil;
import com.ddstation.crud.entity.DtInvsAddDisinfectantEntity;
import com.ddstation.crud.service.DtInvsAddDisinfectantService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class AppSewageController {

	//@Inject private AppSewageService appSewageService;
	@Inject private InvsItemInstitutionService invsItemInstitutionService;
	@Inject private DtInvsAddDisinfectantService dtInvsAddDisinfectantService;
	@Inject private DdPictureMapper ddPictureMapper;

	/**
	 * (污水余氯)获取历史记录
	 * @param params
	 * @return
	 */
	@RequestMapping(value="appinvs_invs_item.do/history", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> invsItemHistoryEvent(
			@RequestBody Map<String, Object> params) {

		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);
		if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))) {
			return retMap;
		}
		String institutionId = params.get("institutionId").toString();
//		String institutionId = "00000012";
		String pageSize = params.get("pageSize").toString();
		String pageNo = params.get("pageNo").toString();

		retMap.put("itemList",dtInvsAddDisinfectantService.selectByHistory(institutionId,Integer.parseInt(pageNo),Integer.parseInt(pageSize)));
		retMap.put("errCode", 0);
		retMap.put("errorMsg", "");
		return retMap;

	}

	/**
	 * (污水余氯)获取记录细节
	 * @param params
	 * @return
	 */
	@RequestMapping(value="appinvs_invs_item.do/detail", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> invsItemDetailEvent(
			@RequestBody Map<String, Object> params) {

		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);
		if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))) {
			return retMap;
		}
		Integer id = (Integer)params.get("id");
		DtInvsAddDisinfectantEntity detailEntity = dtInvsAddDisinfectantService.selectById(id);
		retMap.put("detail",detailEntity);
		List<String> thumbImgList = new ArrayList<String>();
		List<String> originImgList = new ArrayList<String>();
		if (detailEntity.getReportImgId()!=null) {
			for (String m_id:detailEntity.getReportImgId().split(",")) {
				if (!CheckUtil.isEmpty(m_id)) {
					DdPictureEntity picture = ddPictureMapper.selectOne(m_id);
					if (picture.getPath().endsWith("pdf") || picture.getPath().endsWith("PDF")
							|| picture.getPath().endsWith("xls") || picture.getPath().endsWith("xlsx")
							|| picture.getPath().endsWith("XLS") || picture.getPath().endsWith("XLSX")
							|| picture.getPath().endsWith("doc") || picture.getPath().endsWith("docx")
							|| picture.getPath().endsWith("DOC") || picture.getPath().endsWith("DOCX")) {
						continue;
					}
					thumbImgList.add(AliCloudOssUtil.getThumbImgUrl(null, picture.getPath(), "@100h"));
					originImgList.add(AliCloudOssUtil.getThumbImgUrl(null, picture.getPath(), "@800h"));
					//originImgList.add(AliCloudOssUtil.getOriginImgUrl(null, picture.getPath()));
				}
			}
		}
		retMap.put("originImgList",originImgList);
		retMap.put("thumbImgList",thumbImgList);
		retMap.put("errCode", 0);
		retMap.put("errorMsg", "");
		return retMap;

	}

	/**
	 * (污水余氯)获取所有消毒剂名称List
	 * @param params
	 * @return
	 */
	@RequestMapping(value="appinvs_invs_item.do/getAllXdj", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> invsItemAllXdjEvent(
			@RequestBody Map<String, Object> params) {

		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);
		if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))) {
			return retMap;
		}
		String institutionId = params.get("institutionId").toString();
		retMap.put("itemList",invsItemInstitutionService.getInvsItemInstitutionList(5,institutionId));
		retMap.put("errCode", 0);
		retMap.put("errorMsg", "");
		return retMap;
	}

	/**
	 * (污水余氯)获取消毒剂名称List
	 * @param params
	 * @return
	 */
	@RequestMapping(value="appinvs_invs_item.do/getXdj", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> invsItemXdjEvent(
			@RequestBody Map<String, Object> params) {

		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);
		if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))) {
			return retMap;
		}
		String institutionId = params.get("institutionId").toString();
		//retMap.put("list",invsItemService.getInvsItemList(5));
		//判断是否第一次
		if (!(invsItemInstitutionService.selectInvsItemAllCnt(5,institutionId) > 0)) {
			// 初始化数据
			invsItemInstitutionService.init(5,institutionId);
		}
		retMap.put("list",invsItemInstitutionService.selectShowInvsItem(5,institutionId));
		retMap.put("errCode", 0);
		retMap.put("errorMsg", "");
		return retMap;

	}

	/**
	 * (污水余氯)登记
	 * @param params
	 * @return
	 */
	@RequestMapping(value="appinvs_invs_item.do/save", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> addInvsItemEvent(
			@RequestBody Map<String, Object> params) {

		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);

		if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))
				|| CheckUtil.isEmpty(params.get("doctorId"))
				|| CheckUtil.isEmpty(params.get("currentDate"))
				|| CheckUtil.isEmpty(params.get("currentTime"))
				|| CheckUtil.isEmpty(params.get("remainingChlorine"))
				|| CheckUtil.isEmpty(params.get("isAdd"))) {
			return retMap;
		}
		String institutionId = params.get("institutionId").toString();
		Integer doctorId = Integer.parseInt(params.get("doctorId").toString());
//		String institutionId = "00000012";
//		Integer doctorId=12;
		String currentDateStr = params.get("currentDate").toString();
		String currentTimeStr = params.get("currentTime").toString();
		String remainingChlorine = params.get("remainingChlorine").toString();
		Short isAdd = 0;
		if (!CheckUtil.isEmpty(params.get("isAdd")) && Boolean.parseBoolean(params.get("isAdd").toString())) {
			isAdd = 1;
		}
		String invsItemCode = null;
		String quantity = null;
		if (isAdd == 1) {
			invsItemCode = CheckUtil.isEmpty(params.get("invsItemCode")) ? "": params.get("invsItemCode").toString();
			quantity = CheckUtil.isEmpty(params.get("quantity")) ? "":params.get("quantity").toString();
		}
		String reportImgId = CheckUtil.isEmpty(params.get("reportImgId")) ? "":params.get("reportImgId").toString();
		DtInvsAddDisinfectantEntity entity = new DtInvsAddDisinfectantEntity();
		entity.setInstitutionId(institutionId);
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat f2 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		Date currentDate = null;
		Date currentTime = null;
		try {
			currentDate = f.parse(currentDateStr);
			cal.setTime(currentDate);
			entity.setCheckDt(cal.getTime());
			currentTime = f2.parse(currentDateStr + " " + currentTimeStr + ":00");
			cal.setTime(currentTime);
			entity.setCheckTm(cal.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		entity.setRemainingChlorine(Float.valueOf(remainingChlorine));
		entity.setIsAdd(isAdd);
		entity.setDisinfectantCode(invsItemCode == null? null:Integer.parseInt(invsItemCode));
		entity.setQuantity(quantity == null? null:Float.valueOf(quantity));
		entity.setDoctorId(doctorId);
		entity.setReportImgId(reportImgId);
		entity.setStatus((short)0);
		dtInvsAddDisinfectantService.insert(entity);

		retMap.put("errCode", 0);
		retMap.put("errorMsg", "");
		return retMap;

	}

	/**
	 * (污水余氯)新增消毒剂
	 * @param params
	 * @return
	 */
	@RequestMapping(value="appinvs_invs_item.do/addItem", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> invsItemAddItemEvent(
			@RequestBody Map<String, Object> params) {

		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);
		if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))
				|| CheckUtil.isEmpty(params.get("name"))
				|| CheckUtil.isEmpty(params.get("unit"))) {
			return retMap;
		}
		String institutionId = params.get("institutionId").toString();
		String name = params.get("name").toString();
		String unit = params.get("unit").toString();
		List<String> list = invsItemInstitutionService.checkByInvsItemName(5,institutionId,name);
		if (list != null && list.size()>0) {
			retMap.put("errCode", -2);
			return retMap;
		}
		int cnt = invsItemInstitutionService.addInvsItem(5,institutionId,name,unit);
		if (cnt == 1)
			retMap.put("errCode", 0);
		retMap.put("errorMsg", "");
		return retMap;
	}

	/**
	 * (污水余氯)删除消毒剂
	 * @param params
	 * @return
	 */
	@RequestMapping(value="appinvs_invs_item.do/delItem", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> invsItemDelItemEvent(
			@RequestBody Map<String, Object> params) {

		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);
		if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))
				|| CheckUtil.isEmpty(params.get("id"))) {
			return retMap;
		}
		String institutionId = params.get("institutionId").toString();
		Integer id = Integer.parseInt(params.get("id").toString());
		int cnt = invsItemInstitutionService.delInvsItem(5,institutionId,id);
		if (cnt == 1)
			retMap.put("errCode", 0);
		retMap.put("errorMsg", "");
		return retMap;
	}

	/**
	 * (污水余氯)保存要显示的消毒剂
	 * @param params
	 * @return
	 */
	@RequestMapping(value="appinvs_invs_item.do/saveShowXdj", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> invsSaveShowXdjEvent(
			@RequestBody Map<String, Object> params) {

		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);
		if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))
				|| CheckUtil.isEmpty(params.get("ids"))) {
			return retMap;
		}
		String institutionId = params.get("institutionId").toString();
		String ids = params.get("ids").toString();
		invsItemInstitutionService.saveInvsItem(5,institutionId,ids);
		retMap.put("errCode", 0);
		retMap.put("errorMsg", "");
		return retMap;
	}
}
